﻿var ue = UE.getEditor('content', {
    toolbars: [
        ['source','undo', 'redo', 'emotion',
            'selectall', 'link', 'insertcode', 'simpleupload', 'insertimage', 'fontfamily', 'fontsize']],
    enableAutoSave: false,
    maximumWords: 500,
    autoHeightEnabled: false,
    autoFloatEnabled: false
});

var page = 1;

$(function () {
    //$('.m-main').parent().css('background', '#F2FDDB');
    getWordList();
})

function sendWord() {
    if (!ue.hasContents()) {
        layerAlert('内容还没写呢！', function () { ue.focus(); });
        return;
    }
    $.post('/words/release', { id: 0, content: $.htmlEncode(ue.getContent().replace("<p></p>", "")) },
        function (o) {
            if (o.State == 1) {
                layerMsg('发表成功！');
                changeCount(1);
                getWordList();
                closeSendView();
            } else {
                layerAlert('发表失败：' + o.Message);
            }
        }, "json");
}

function changeHeight(o) {
    $(o).css("height", "50px");
    $(o).next("span").first().css('display', 'inline-block');
}

function changeBack(o) {
    if (!$.trim($(o).val())) {
        $(o).css("height", "20px");
        $(o).next("span").first().css('display', 'none');
    } else {
        $(o).css("height", "50px");
    }
}

function setTxtFocus(id, name) {
    var top = $('#tarea-' + id).offset().top;
    $(window).scrollTop(top - 100);
    $('#tarea-' + id).focus();
    if (name != undefined) {
        $('#tarea-' + id).val('@' + name + '：');
    }
}

function delComment(id) {
    layerConfirm('确定删除这条回复吗？', function () {
        $.post('/words/DelComment', { id: id }, function (o) {
            if (o.State == 1) {
                layerMsg('删除成功！');
                changeCount(-1);
                getWordList(page);
            } else {
                layerAlert('删除失败：' + o.Message);
            }
        })
    });
}

function changeCount(n) {
    var old = $('#hTotal').val();
    var now = parseInt(old) + n;
    $('#hTotal').val(now);
    $('#hpage').val(now % 15 == 0 ? (now / 15) : (now / 15 + 1));
}
var view = null;
var viewIndex = null;
function showSendView(id) {
    if (view == null || view == undefined) {
        view = $('#dvSendView').html();
    }
    var title = id > 0 ? "修改内容" : "编辑内容";
    if (id > 0) {
        $('#hID').val(id);
        openIframe('/words/_wordEdit/' + id, title, 800, 410);
        return;
    }
    layerOpen(title, view, 800, 380, function (vid) {
        viewIndex = vid;
    });
}

function closeSendView() {
    ue.setContent('');
    $('#hID').val(0);
    layer.close(viewIndex);
}

function agree(id, count, a) {
    doAgree('/words/Agree', id, count, a);
}

function agreeReply(id, count, a) {
    doAgree('/words/AgreeReply', id, count, a);
}

function sendComment(id) {
    if (!layerCheckLogin()) {
        return;
    }
    var content = $.htmlEncode($("#tarea-" + id).val());
    $.post('/words/addcomment', { id: id, content: content }, function (o) {
        if (o.State == 1) {
            getWordList();
            $("#tarea-" + id).val('');
            changeBack($("#tarea-" + id));
        } else {
            layerAlert('评论失败：' + o.Message);
        }
    })
}

function deleteWord(id) {
    layerConfirm('确定删除这条记录吗？', function () {
        $.post('/words/deleteWord', { id: id }, function (o) {
            if (o.State == 1) {
                layerMsg('删除成功！');
                changeCount(-1);
                getWordList(page);
            } else {
                layerAlert('删除失败：' + o.Message);
            }
        })
    });
}

function getWordList(curr) {
    $('#dvWordList').html("加载中，请稍候...");
    page = curr;
    $.get('/words/_WordAjaxList', { page: curr },
        function (html) {
            $('#dvWordList').html(html);
            doUeParse();
            laypage({
                cont: $('#laypager'), //容器。值支持id名、原生dom对象，jquery对象。
                pages: $('#hpage').val(), //通过后台拿到的总页数
                curr: curr || 1, //当前页
                //skin: 'molv',
                jump: function (obj, first) { //触发分页后的回调
                    if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                        getWordList(obj.curr);
                        //doUeParse();
                    }
                }
            });
        }, "html");
}